﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using System.IO;

namespace Quản_Lý_Thư_Viện
{
    public partial class frmNhapDauSach : Form
    {
        public frmNhapDauSach()
        {
            InitializeComponent();
        }
        DausachControl control = new DausachControl();
        ConhieuControl cncontrol = new ConhieuControl();
        ctpnsControl pnscontrol = new ctpnsControl();
        CTPMcontrol ctpmscontrol = new CTPMcontrol();
        SachControl scontrol = new SachControl();
        bool kt = false;
        bool bInserted = false;
        public static string timmds;

        private void frmNhapDauSach_Load(object sender, EventArgs e)
        {
            TheloaiControl tl = new TheloaiControl();
            DataTable dt = tl.LayDSTheloai();
            cboMaTheLoai.DataSource = dt;
            cboMaTheLoai.ValueMember = "MATHELOAI";
            cboMaTheLoai.DisplayMember = "TENTHELOAI";
            dgvDauSach.DataSource = control.LayDSDausach();
            TacgiaControl tg = new TacgiaControl();
            DataTable dt2 = tg.LayDSTacgia();
            cboTacGia1.DataSource = dt2;
            cboTacGia1.ValueMember = "MATG";
            cboTacGia1.DisplayMember = "TENTG";
        }
        private void ShowControl(bool bShow)
        {
            //txtMaDauSach.Enabled = bInserted;
            txtNamXuatBan.Enabled = bShow;
            txtNhaXuatBan.Enabled = bShow;
            txtTenSach.Enabled = bShow;
            txtTriGia.Enabled = bShow;
            cboMaTheLoai.Enabled = bShow;
            txtHinhAnh.Enabled = bShow;
            btn_browse.Enabled = bShow;
            btnSave.Enabled = bShow;
            
        }
        private void Clear()
        {
            txtMaDauSach.Text = "";
            txtNamXuatBan.Text = "";
            txtNhaXuatBan.Text = "";
            txtTenSach.Text = "";
            txtTriGia.Text = "";
        }
        private void laythongtindausach(ref DausachInfo info)
        {
            
                FileStream fStream = new FileStream(txtHinhAnh.Text, FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(fStream);
                info.Hinhanh = br.ReadBytes((int)fStream.Length);
            
            info.Madausach = txtMaDauSach.Text;
            info.Matheloai = cboMaTheLoai.SelectedValue.ToString();
            info.Tensach = txtTenSach.Text;
            info.Nxb = txtNhaXuatBan.Text;
            info.Namxb = txtNamXuatBan.Text;
            info.Trigia = Convert.ToInt32(txtTriGia.Text);
            //info.Tacgia1 = cbb_tacgia1.SelectedValue.ToString();
        }
        private void laythongtinctpns(ref PhieunhapsachInfo info)
        {
            info.Madausach = dgvDauSach.CurrentRow.Cells["MADAUSACH"].Value.ToString();
        }
        private void laythongtinconhieu(ref ConhieuInfo info)
        {
            info.Madausach = txtMaDauSach.Text;
            info.Matg = cboTacGia1.SelectedValue.ToString();
        }
        
        private void btnOk_Click(object sender, EventArgs e)
        {
            DausachInfo dsinfo = new DausachInfo();
            if (
                   
                    txtNamXuatBan.Text == "" ||
                    txtNhaXuatBan.Text == "" ||
                    txtTenSach.Text == "" ||
                    txtTriGia.Text == ""
                    )
            {
                MessageBox.Show("Chưa Nhập Đầy Đủ Các Thành Phần Yêu Cầu !", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {

                if (bInserted)
                {

                    if (txtHinhAnh.Text == "")
                    {
                        MessageBox.Show("Vui Lòng Chọn Hình Ảnh ! ");
                    }
                    else
                    {
                        laythongtindausach(ref dsinfo);
                        if (kiemtra(txtMaDauSach.Text.Trim()) == true)
                        {
                            MessageBox.Show("Trùng Mã Đầu Sách !");
                        }
                        else
                        {

                            control.insertdausach(dsinfo);
                            MessageBox.Show("Thêm Đầu Sách Thành Công");
                            dgvDauSach.DataSource = control.LayDSDausach();
                            btnThemTacGia.Enabled = true;
                            bInserted = false;
                            ShowControl(false);
                            btnSua.Enabled = true;
                            btnThem.Enabled = true;
                            btnThemTacGia.Enabled = true;
                            cboTacGia1.Enabled = true;
                        }
                    }
                }

                else
                {
                    
                        laythongtindausach(ref dsinfo);
                        control.updatedausach(dsinfo);
                        MessageBox.Show("Sửa Dữ Liệu Thành Công");
                        dgvDauSach.DataSource = control.LayDSDausach();
                        ShowControl(false);
                        btnSua.Enabled = true;
                        btnThem.Enabled = true;
                    
                }
            }
        }

        private void dgvDauSach_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvDauSach.RowCount > 0)
            {
                txtMaDauSach.Text = dgvDauSach.CurrentRow.Cells["MADAUSACH"].Value.ToString();
                txtTenSach.Text = dgvDauSach.CurrentRow.Cells["TENSACH"].Value.ToString();
                txtNhaXuatBan.Text = dgvDauSach.CurrentRow.Cells["NXB"].Value.ToString();
                txtNamXuatBan.Text = dgvDauSach.CurrentRow.Cells["NAMXB"].Value.ToString();
                txtTriGia.Text = dgvDauSach.CurrentRow.Cells["TRIGIA"].Value.ToString();
                cboMaTheLoai.SelectedValue = dgvDauSach.CurrentRow.Cells["MATHELOAI"].Value.ToString();
                //cbb_tacgia1.SelectedValue = dgv_dausach.CurrentRow.
            }
        }
        public static string a,madausachtemp;

        private void btnXoa_Click(object sender, EventArgs e)
        {
            frmNhapSach frmA = new frmNhapSach();
            CTPMinfo pminfo = new CTPMinfo();
            madausachtemp = dgvDauSach.CurrentRow.Cells["MADAUSACH"].Value.ToString();
            DataTable dt1 = new DataTable();
            dt1 = scontrol.laysach();
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                DataRow dr = dt1.Rows[i];
                pminfo.Masach = dr["MASACH"].ToString();
                ctpmscontrol.deletectpm(pminfo);
            }
            
            a = txtMaDauSach.Text;
            frmA.deletetempsach();
            DausachInfo dsinfo = new DausachInfo();
            laythongtindausach(ref dsinfo);
            PhieunhapsachInfo pnsinfo = new PhieunhapsachInfo();
            laythongtinctpns(ref pnsinfo);
            pnscontrol.deletectpns(pnsinfo);
            ConhieuInfo cninfo = new ConhieuInfo();
            cninfo.Madausach = dgvDauSach.CurrentRow.Cells["MADAUSACH"].Value.ToString();
            cncontrol.deleteconhieutg(cninfo);
            control.deletedausach(dsinfo);
            MessageBox.Show("Xoa thanh cong");
            dgvDauSach.DataSource = control.LayDSDausach();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            bInserted = false;
            ShowControl(true);
            btnThem.Enabled = false;
            btnSua.Enabled = false;
            cboTacGia1.Enabled = true;
            btnThemTacGia.Enabled = true;
        }

        private void btnThemTacGia_Click(object sender, EventArgs e)
        {
            ConhieuInfo cninfo = new ConhieuInfo();
            laythongtinconhieu(ref cninfo);
            cncontrol.insertconhieu(cninfo);
            MessageBox.Show("Them thanh cong 1 tac gia ");
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            bInserted = true;
            Clear();
            ShowControl(true);
            btnSua.Enabled = false;
            btnThem.Enabled = false;
            btnThemTacGia.Enabled = false;
            cboTacGia1.Enabled = true;
            int i = dgvDauSach.Rows.Count + 1;
            txtMaDauSach.Text = "DS" + i + "";
        }
        private Boolean kiemtra(string ma)
        {

            for (int j = 0; j < dgvDauSach.RowCount ; j++)
            {
                if (ma == dgvDauSach.Rows[j].Cells[0].Value.ToString())//cells[chỉ số so sánh]
                {
                    kt = true;
                    break;
                }
                else
                    kt = false;
            }
            return kt;
        }

        private void btn_browse_Click(object sender, EventArgs e)
        {
            OpenFileDialog odf = new OpenFileDialog();
            odf.Filter = "Image files|*.jpg";
            if (odf.ShowDialog() == DialogResult.OK)
                txtHinhAnh.Text = odf.FileName;
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            if (dgvDauSach.RowCount > 0)
            {
                txtMaDauSach.Text = dgvDauSach.CurrentRow.Cells["MADAUSACH"].Value.ToString();
                txtTenSach.Text = dgvDauSach.CurrentRow.Cells["TENSACH"].Value.ToString();
                txtNhaXuatBan.Text = dgvDauSach.CurrentRow.Cells["NXB"].Value.ToString();
                txtNamXuatBan.Text = dgvDauSach.CurrentRow.Cells["NAMXB"].Value.ToString();
                txtTriGia.Text = dgvDauSach.CurrentRow.Cells["TRIGIA"].Value.ToString();
                cboMaTheLoai.SelectedValue = dgvDauSach.CurrentRow.Cells["MATHELOAI"].Value.ToString();
                //cbb_tacgia1.SelectedValue = dgv_dausach.CurrentRow.
            }
            bInserted = false;
            ShowControl(false);
            btnThem.Enabled = true;
            btnSua.Enabled = true;
            btnThemTacGia.Enabled = false;
            cboTacGia1.Enabled = false;
        }

        private void KiemTraNhapSo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Yêu Cầu Nhập Số");
            }
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Escape))
            {
                this.Close();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            timmds = textBox3.Text;
            dgvDauSach.DataSource = control.timmds();
        }      
        
    }
}

